<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars 
define("PRIMARY_KEY","nobkt");
$PRIMARY_KEY    = PRIMARY_KEY;
$PRIMARY_VALUE  = GetParam($PRIMARY_KEY,"");
$FileName       = "ais_transaction_advsummary.php";
$FileTemplate   = "ais_transaction_advsummary_form.html";
$SubHeaderTitle = "Transaction Advance - Summary Advance";
$TableName		= "ais_advance";
$HTMLFormName   = "AdvanceSummaryForm";
$MaxGrid        = "100";
//$HTMLFormName2  = "BudgetForm";

$FormAction    = $FileName . "?" . AddParam(GetQueryString("QueryString", ""),"form","$HTMLFormName");
$FormEncType   = "application/x-www-form-urlencoded";
//$PMode         = "";

//Initiate Grid Fields;
$FormFields    = array("receipt_id"=>"","urut"=>"0","uraian"=>"0","uraian_tools"=>"0","jumlah"=>"number","skada"=>"0","untush"=>"0","princ"=>"","vessel"=>"","voyage"=>"","cbm"=>"","jenis"=>"","dol"=>"");

//buildsqlfield ("$TableName", $FormFields);

//Leave Here Untouch
$FormCName     = BuildHTMLField($FormFields,"name",1);
$FormCValue    = BuildHTMLField($FormFields,"value",1);

//************ FORM ACTION **************
if (trim(GetParam("form",""))==trim($HTMLFormName)) {
	if (GetParam("FormAction","")=="Save") {
		list($FDay,$FMonth,$FYear) = explode("/",GetParam("tglkwit",""));
		$tglkwit = "$FYear-$FMonth-$FDay";
		$rate    = StrToNumber (GetParam("rate",""));
	
		if (trim(GetParam("nokwit",""))=="AUTO NUMBERED BY SYSTEM")	{
			$TransNo = SaveNumber("KW", GetParam("thbl",""), GetParam("tglkwit",""));
			FormSave($TableName, "uraian", $FormFields, array("ledger"=>GetPeriod(GetParam("tglkwit","")),"thbl"=>GetParam("thbl",""),"debnam"=>GetParam("debnam",""),"nokwit"=>$TransNo,"npwp"=>GetParam("npwp",""),"tglkwit"=>"$tglkwit","remark1"=>GetParam("remark1",""),"remark2"=>GetParam("remark2",""),"remark3"=>GetParam("remark3",""),"curr"=>GetParam("curr",""),"rate"=>$rate,"debtur"=>GetParam("debtur",""),"ktrasl"=>GetOfficeID()),"receipt_id");
	    }
		else {
			$TransNo = GetParam("nokwit","");
			FormSave($TableName, "uraian", $FormFields, array("ledger"=>GetPeriod(GetParam("tglkwit","")),"debnam"=>GetParam("debnam",""),"nokwit"=>$TransNo,"npwp"=>GetParam("npwp",""),"tglkwit"=>"$tglkwit","remark1"=>GetParam("remark1",""),"remark2"=>GetParam("remark2",""),"remark3"=>GetParam("remark3",""),"curr"=>GetParam("curr",""),"rate"=>$rate,"debtur"=>GetParam("debtur",""),"ktrasl"=>GetOfficeID()),"receipt_id");
		}
		
		DeleteRecord ("txtCheck",$MaxGrid);
		header("Location: $FileName?PageMode=Form&".PRIMARY_KEY."=".$TransNo);
		exit;
		
	}
	if (GetParam("FormAction","")=="Delete") {
//		print "Deleted"."<br>\n";
		DeleteRecord ("txtCheck",$MaxGrid);
	}
}

//Field Verification 
$Verification  = "False";
$SessionVar    = md5(microtime());
//$SessionVar    = "Stuck";
$VerifyFields = array("thbl"=>"Source","nokwit"=>"ID No.","npwp"=>"NPWP","tglkwit"=>"Transaction Date","remark1"=>"Remark","curr"=>"Currency");
VerifyHTML ($HTMLFormName,$VerifyFields,$SessionVar,array("debtur"=>"Received From"));

if (GetParam(PRIMARY_KEY,"")) {
	$SQLCount     = "SELECT count(*) FROM `$TableName`";
	$SQLCount    .= "WHERE ".PRIMARY_KEY."='".$PRIMARY_VALUE."'";
	$Count        = $DBConnection->dbc->get_var($SQLCount);	
} else $Count=0;

$NewDataStart = $Count+1;
$InitFunction = "Masking();Masking2();";
$VerifyGrids = array("uraian"=>"0","uraian_tools"=>"0","jumlah"=>"number","skada"=>"0","untush"=>"0");
$Masking1 = BuildMasking ($HTMLFormName,MutatorFields($FormFields,($MaxGrid+$Count)));
$Masking2 = BuildMasking ($HTMLFormName,array("tglkwit"=>"date","rate"=>"number"),"2");
$JSMasks  = $Masking1.$Masking2;

Init($SessionVar,$JSMasks);
FormGridHTML ($HTMLFormName,$VerifyGrids,$Count,$MaxGrid,$SessionVar);



//************ SET FORM MODE ***************
if (GetParam(PRIMARY_KEY,"")) $FormMode = "Edit";
else $FormMode = "Add";

//******* DATABASE GRID QUERY ***********

$SQL = "SELECT ".BuildFields($FormFields)." FROM `$TableName`";
if (GetParam(PRIMARY_KEY,"") && $FormMode="Edit") {
    $SQLGrid  = $SQL."WHERE ".PRIMARY_KEY." ='".GetParam(PRIMARY_KEY,"")."'";
    $SQLGrid .= " ORDER BY ".PRIMARY_KEY;
}





//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);

//********************************* HTML Header ******************************************
//Select Currency Table
$SQLCurr = "SELECT * FROM tblcurrency";
//Select Department:
$SQLDept = "SELECT * FROM tbldept";
//Select Receipt:
$SQLReceipt = "SELECT * FROM tblreceipt";

$HeaderFields = array("thbl"=>"0","debnam"=>"0","nokwit"=>"0","npwp"=>"","tglkwit"=>"date","remark1"=>"","remark2"=>"","remark3"=>"","curr"=>"","rate"=>"number","debtur"=>"");
//Leave Here Untouch
$FormCName     = BuildHTMLField($HeaderFields,"name",1);
$FormCValue    = BuildHTMLField($HeaderFields,"value",1);
$SQLHeader     = "SELECT ".BuildFields($HeaderFields)." FROM $TableName ";

if (GetParam(PRIMARY_KEY,"") && $FormMode="Edit") $SQLHeader .= "WHERE ".PRIMARY_KEY."='".GetParam(PRIMARY_KEY,"")."' AND ktrasl='".GetOfficeID()."'";
$TBS->MergeBlock("currformblk",$DBMysql,$SQLCurr);
$TBS->MergeBlock("receiptblk",$DBMysql,$SQLReceipt);

// HTML Value
$TBS->MergeBlock("headbln",$FormCName);
if (GetParam(PRIMARY_KEY,"")) {$TBS->MergeBlock("headblv",$DBMysql,$SQLHeader);}
else $TBS->MergeBlock("headblv",$FormCValue);



//********************************* HTML GRID *********************************************
//HTML Grid
$Result = $DBConnection->dbc->get_results($SQLGrid,ARRAY_A);
$Result  = BuildEmptyGrid ($FormFields, $MaxGrid, $Result);

$TBS->MergeBlock("deptblk",$DBMysql,$SQLDept);
$TBS->MergeBlock("blg",$Result);
$TBS->Show();


//*************** ACTION FUNCTION *******************
function FormInsert() {
	global $DBConnection,$FileName;
	$DBConnection->Insert();
	header("Location: $FileName?PageMode=Form");
	exit;
}

function FormUpdate() {
	global $DBConnection;
	$DBConnection->Update();
}

function FormDelete() {
	global $DBConnection,$FileName,$TableName;
	$DBConnection->deleteGrid("$TableName","nobkt");
	header("Location: $FileName");
	exit;	
}

function FormSearch () {

}

function FormCancel () {
	global $FileName;
	header("Location: ".$FileName);
	exit;
}

function FormSave ($table, $keyField, $FormFields, $SpecialCase="", $PrimaryKey) {
	global $FileName, $MaxGrid, $DBConnection;
	$TotalData = $MaxGrid * 2;	
	$NewDataStart = GetParam("NewData","0");
	for ($a=1;$a<=$TotalData;$a++) {
		foreach ($FormFields as $key=>$formatting) {
			if (!GetParam($keyField.$a,"")) {
				$EndOfData = 1;
				break;
			}
			$value = GetParam($key.$a,"");
			if ($formatting=="number") $value = str_replace(",","",$value);
			if ($formatting=="date") {
				list($day,$month,$year) = explode("/",$value);
				$value = "$year-$month-$day";
		    }
			$DBConnection->add_data($table,$key,$value);
		}
		$rate = StrToNumber(GetParam("rate",""));
		$jmh  = StrToNumber(GetParam("jumlah".$a,""));
		$rupiah = $rate * $jmh;
		$DBConnection->add_data($table,"rupiah",$rupiah);
		if ($EndOfData) {
			break;
	    }
		if (strlen($SpecialCase)) {
			foreach ($SpecialCase as $key => $value) {
			//	print "add_data($table,$key,$value);<br>";
				$DBConnection->add_data($table,$key,$value);
			}
		}
//		print "$a < $NewDataStart<br>";
		if ($a < $NewDataStart) {
			 if (strlen($PrimaryKey)) {
			    $DBConnection->UpdateGrid($PrimaryKey,GetParam($PrimaryKey.$a,""));
		 	 } else {
			    $DBConnection->UpdateGrid($keyField,GetParam($keyField.$a,""));
		     }
		} else {
			$DBConnection->InsertGrid();
	    }
	}
}
//*************** USER FUNCTION *******************
function buildsqlfield ($table, $arrayfield) {
		global $DBConnection;
		for ($x=0;$x<count($arrayfield);$x++) {
			$DBConnection->add_htmlfield($table,$arrayfield[$x],GetParam($arrayfield[$x],""));
		}
}

function DeleteRecord ($sequence_name,$PageSize) {
	global $DBConnection,$FileName,$TableName;	
	for ($x=1;$x<=$PageSize;$x++) {				
		if (GetParam($sequence_name.$x,""))	{
			$DBConnection->deleteGrid("$TableName","receipt_id",$sequence_name.$x);
	    }
	}
}

function BuildFields ($FormFields) {
		global $DBConnection;
		$str = "";
		foreach ($FormFields as $key=>$formatting) {
			if (strlen($str)) $str .= ",$key";
			else $str = "$key";
		}
		return $str;
}

function MutatorFields ($FormFields, $MaxGrid) {
		global $DBConnection;
		$array_fields = array();
		for ($x=0;$x<$MaxGrid;$x++) {
			foreach ($FormFields as $key=>$formatting){
				$a=$x+1;
				$array_fields[$key.$a] = "$formatting";
			}			
		}
		return $array_fields;
}
?>